﻿#include "ElementStatusBar.h"
#include <QPushButton>
#include <QHBoxLayout>
#include <QSpacerItem>
#include <QLineEdit>
#include <qtoolbutton.h>
#include <QLabel>
#include <QGridLayout>
//////////////////////////////////////////
/// ElementStatusBarPrivate
////////////////////////////////////////////
struct ElementStatusBarPrivate{
    QGridLayout *layout=nullptr;
    QAction *left =nullptr;
    QAction *right=nullptr;
    QToolButton *leftBT=nullptr;
    QToolButton *rightBT=nullptr;
    QWidget* midAere = nullptr;
    QLabel* leftDisplayer=nullptr;
    QLabel* rightDisplayer=nullptr;
    QWidget* midDisplayer=nullptr;
};

//////////////////////////////////////////
/// ElementStatusBar
////////////////////////////////////////////
ElementStatusBar::ElementStatusBar(QWidget *parent)
    :QStatusBar{parent}
    ,mPrivate(new ElementStatusBarPrivate)
{

    this->setFixedHeight(26);
    this->setContentsMargins(0,0,0,0);
    mPrivate->leftBT = new QToolButton(this);
    mPrivate->leftBT->setFixedSize(20,20);
    this->addWidget(mPrivate->leftBT);

    mPrivate->rightBT = new QToolButton(this);
    mPrivate->rightBT->setFixedSize(20,20);
    this->addPermanentWidget(mPrivate->rightBT);

    mPrivate->midAere = new QWidget(this);
    mPrivate->midAere->setObjectName("ElementStatusBarMidAere");

    QGridLayout *layout = new QGridLayout(mPrivate->midAere);
    layout->setSpacing(0);
    mPrivate->layout = layout;
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setRowMinimumHeight(0,this->height());
    layout->setRowStretch(0,0);

    layout->setColumnMinimumWidth(0,160);
    layout->setColumnStretch(0,0);
    layout->setColumnMinimumWidth(2,160);
    layout->setColumnStretch(2,0);
    layout->setColumnMinimumWidth(1,160);
    layout->setColumnStretch(1,3);

    mPrivate->midAere->setLayout(layout);
    this->insertWidget(1,mPrivate->midAere,1);

    QLabel *leftLabel = new QLabel(this);
    setLeftDisplayer(leftLabel);
    QLabel *rightLabel = new QLabel(this);
    setRightDisplayer(rightLabel);
    QLineEdit *msgLabel = new QLineEdit(this);
    setMidDisplayer(msgLabel);

    this->setStyleSheet(
        "QStatusBar{"
        "background-color: rgb(64, 80, 141);"//
        "}"
        "QStatusBar QWidget{"
        "background-color: rgb(64, 80, 141);"//
        "}"
        "QStatusBar #ElementStatusBarMidAere{"
        "background-color: rgb(64, 80, 141);"
        "}"
        "QStatusBar QToolButton{"
        "background-color: rgb(64, 80, 141);"
        "border-radius:5px;"
        "}"
        "QStatusBar QToolButton:hover{"
        "background-color: rgb(100, 100, 100);"
        "border-radius:5px;"
        "}"
        "QStatusBar QToolButton:pressed{"
        "background-color: rgb(70, 70, 70);"
        "border-radius:5px;"
        "}"
        "QStatusBar QToolButton:checked{"
        "background-color: rgb(70, 70, 70);"
        "border-radius:3px;"
        "}"
    );

    QObject::connect(this,&ElementStatusBar::messageChanged,this,
        [this](const QString& str){
            this->showMessageOnLeft("  "+str);
        });
}

ElementStatusBar::~ElementStatusBar()
{
    if(mPrivate)
        delete mPrivate;
}

void ElementStatusBar::setLeftAction(QAction *action)
{
    mPrivate->left = action;
    mPrivate->leftBT->setDefaultAction(mPrivate->left);
}

void ElementStatusBar::setRightAction(QAction *action)
{
    mPrivate->right = action;
    mPrivate->rightBT->setDefaultAction(mPrivate->right);
}

QWidget *ElementStatusBar::setLeftDisplayer(QLabel *left,int width)
{
    width = width <=0? 160:width;
    left->setMinimumWidth(width);
    QWidget *oldLeft=mPrivate->leftDisplayer;
    if(oldLeft)
        mPrivate->layout->removeWidget(oldLeft);
    mPrivate->layout->addWidget(left,0,0,1,1);
    mPrivate->leftDisplayer = left;
    return oldLeft;
}

QWidget *ElementStatusBar::setRightDisplayer(QLabel *right, int width)
{
    width = width <=0? 160:width;
    right->setMinimumWidth(width);
    QWidget *oldRight=mPrivate->rightDisplayer;
    if(oldRight)
        mPrivate->layout->removeWidget(oldRight);
    mPrivate->layout->addWidget(right,0,2,1,1);
    mPrivate->rightDisplayer = right;
    return oldRight;
}

QWidget *ElementStatusBar::setMidDisplayer(QWidget *mid, int width)
{
    QWidget *oldMid = mPrivate->midDisplayer;
    if(oldMid)
        mPrivate->layout->removeWidget(oldMid);
    mPrivate->layout->addWidget(mid,0,1,1,1);
    mPrivate->midDisplayer = mid;
    return oldMid;
}

void ElementStatusBar::showMessageOnLeft(const QString &msg, int timeout)
{
    mPrivate->leftDisplayer->setText("  "+msg);
}

void ElementStatusBar::showMessageOnRight(const QString &msg,int timeout)
{
    mPrivate->rightDisplayer->setText("  "+msg);
}
